// pages/remainder/remainder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ret: '0',
    firstNum: '',
    secondNum: '',
    sign: '',
    equation: '', //算式
    isFirstNumOver: false,
    canCount: false
  },

  //数组按键点击事件
  numClick: function(event) {
    let value = event.currentTarget.dataset.value;
    if (this.data.ret == '0' && value == '0') {
      return;
    }
    let ret = this.data.ret;
    if (ret == '0') {
      ret = '';
    }
    if (!this.data.isFirstNumOver) {
      let firstNum = this.data.firstNum;
      firstNum += value;
      console.log(firstNum)
      this.setData({
        firstNum: parseFloat(firstNum)
      })
      this.setData({
        ret: firstNum
      })
    } else {
      let secondNum = this.data.secondNum;
      secondNum += value;
      console.log('按下数字键的第二个数是', secondNum)
      this.setData({
        secondNum: parseFloat(secondNum)
      })
      this.setData({
        ret: secondNum
      })
    }
  },

  //计算按键事件
  count: function(event) {
    let equation= '';
    let val = event.currentTarget.dataset.value;
    console.log('第一个数是', this.data.firstNum)
    console.log('第二个数是', this.data.secondNum)
    let firstNum = this.data.firstNum;
    let secondNum = this.data.secondNum;
    // 赋值第一个运算符号
    if (this.data.sign == '') {
      this.setData({
        sign: val
      })
      equation = firstNum.toString() + this.data.sign;
    } else {
      equation = this.data.equation; //算式
      if (equation.substr(equation.length - 1) == this.data.sign) {
        equation = equation.substr(0, equation.length - 1);
      }
      equation = equation + this.data.sign + secondNum.toString();
    }
    if (secondNum == '') {
      secondNum = 0;
    }
    let ret;
    if (this.data.sign != val) {
      console.log('走这里了', this.data.sign)
      ret = this.calculate(this.data.sign, firstNum, secondNum);
    } else {
      ret = this.calculate(val, firstNum, secondNum);
    }
    //如果此时符号是特殊的mod则第一次计算结果仍为第一个数
    if(this.data.sign == 'mod'){
      ret = this.data.firstNum;
    }
    this.setData({
      isFirstNumOver: true,
      canCount: true,
      ret: ret,
      secondNum: '',
      firstNum: ret,
      sign: val,
      equation: equation
    })
  },

  // //计算
  calculate: function(sign, num1, num2) {
    let ret;
    console.log('符号是', sign)
    if (num2 == '') {
      num2 = 0;
    }
    if (num2 == 0 && (sign == '*' || sign == '÷')) {
      console.log('走这里了')
      num2 = 1;
    }
    console.log('三个参数分bie是', sign, num1, num2)
    if (sign == '+') {
      ret = parseFloat(num1) + parseFloat(num2);
    } else if (sign == '-') {
      ret = parseFloat(num1) - parseFloat(num2);
    } else if (sign == '*') {
      ret = parseFloat(num1) * parseFloat(num2);
    } else if (sign == '÷') {
      ret = parseFloat(num1) / parseFloat(num2);
    }else if(sign == 'mod'){
      ret = parseFloat(num1) % parseFloat(num2);
    }
    return ret;
  },

  // //减法
  // sub: function(num1, num2){
  //   if (num2 == '') {
  //     num2 = 0;
  //   }
  //   let ret = parseFloat(num1) - parseFloat(num2);
  //   return ret;
  // },

  // //加法
  // add: function(num1, num2){
  //   if (num2 == '') {
  //     num2 = 0;
  //   }
  //   let ret = parseFloat(num1) + parseFloat(num2);
  //   return ret;
  // },

  //等于点击事件
  equal: function() {
    if (this.data.canCount) {
      console.log('第一个数是', this.data.firstNum)
      console.log('第二个数是', this.data.secondNum)
      if (this.data.secondNum == 0) {
        return;
      }
      if (this.data.isFirstNumOver) {
        let equation = '';
        let secondNum = parseFloat(this.data.ret);
        let ret;
        // if (this.data.sign == '+') {
        //   ret = this.data.firstNum + secondNum;
        // } else if (this.data.sign == '-') {
        //   ret = this.data.firstNum - secondNum;
        // }
        equation = this.data.equation; //算式
        if (equation.substr(equation.length - 1) == this.data.sign) {
          equation = equation.substr(0, equation.length - 1);
        }
        equation = equation + this.data.sign + secondNum.toString();
        ret = this.calculate(this.data.sign, this.data.firstNum, secondNum)
        this.setData({
          ret: ret,
          equation: equation
        })
      }
      this.setData({
        canCount: false
      })
    } else {
      console.log('不能计算')
    }
  },

  //CE按键事件
  clear: function() {
    this.setData({
      ret: '0',
      firstNum: '',
      secondNum: '',
      sign: '',
      isFirstNumOver: false,
      canCount: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})